package com.zjt.myjiakao.fragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zjt.myjiakao.R;
import com.zjt.myjiakao.dialog.ImgDialog;
import com.zjt.myjiakao.pojo.AllErrorPoJp;
import com.zjt.myjiakao.pojo.KeMuYiPoJo;

import org.litepal.LitePal;

import java.util.List;
/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ReadFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ReadFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private KeMuYiPoJo.ResultBean    subDataBean;
    private View view;
    private Activity activity;
    private int  num;
    public ReadFragment() {
    // Required empty public constructor
    }
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     * @return A new instance of fragment ReadFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ReadFragment newInstance(KeMuYiPoJo.ResultBean subDataBean, int position) {
        ReadFragment fragment = new ReadFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, subDataBean);
        args.putSerializable(ARG_PARAM2, position+1);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=getActivity();
        if (getArguments() != null) {
            subDataBean = (KeMuYiPoJo.ResultBean ) getArguments().getSerializable(ARG_PARAM1);
            num=(int)getArguments().getSerializable(ARG_PARAM2);
        }
        //Log.e("111",subDataBean.toString()+"           "+System.currentTimeMillis());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_read, container, false);
        initView();
        // Inflate the layout for this fragment
        return view;
    }
    private void initView() {
        TextView tv_question = (TextView) view.findViewById(R.id.tv_question);
        TextView question1 = (TextView) view.findViewById(R.id.question1);
        TextView question2 = (TextView) view.findViewById(R.id.question2);
        TextView question3 = (TextView) view.findViewById(R.id.question3);
        TextView question4 = (TextView) view.findViewById(R.id.question4);
        LinearLayout one_lin=(LinearLayout) view.findViewById(R.id.one_lin);
        LinearLayout one_lin2=(LinearLayout) view.findViewById(R.id.one_lin2);
        LinearLayout one_lin3=(LinearLayout) view.findViewById(R.id.one_lin3);
        LinearLayout one_lin4=(LinearLayout) view.findViewById(R.id.one_lin4);
        final LinearLayout jieshi_info=(LinearLayout) view.findViewById(R.id.jieshi_info);
        final LinearLayout error_info=(LinearLayout) view.findViewById(R.id.error_info);
        final LinearLayout true_info=(LinearLayout) view.findViewById(R.id.true_info);
        LinearLayout img=(LinearLayout) view.findViewById(R.id.img);
        ImageView img2=(ImageView)view.findViewById(R.id.img2);
        final TextView error_info2=(TextView) view.findViewById(R.id.error_info2);
        final TextView jieshi_info2=view.findViewById(R.id.jieshi_info2);
        final ImageView img_1=(ImageView) view.findViewById(R.id.img_1);
        final ImageView img_2=(ImageView) view.findViewById(R.id.img_2);
        final ImageView img_3=(ImageView) view.findViewById(R.id.img_3);
        final ImageView img_4=(ImageView) view.findViewById(R.id.img_4);
        tv_question.setText(num+"："+subDataBean.getQuestion());
        question1.setText(subDataBean.getItem1());
        question2.setText(subDataBean.getItem2());
        if (subDataBean.getUrl().equals("")){
            img.setVisibility(View.GONE);
        }else {
            img.setVisibility(View.VISIBLE);
            Glide.with(activity).load(subDataBean.getUrl()).into(img2);
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImgDialog.show(getContext(),subDataBean.getUrl());
                }
            });
        }
        if(subDataBean.getItem3().equals("")){
            one_lin3.setVisibility(View.GONE);
            one_lin4.setVisibility(View.GONE);
        }else {
            one_lin3.setVisibility(View.VISIBLE);
            one_lin4.setVisibility(View.VISIBLE);
            question3.setText(subDataBean.getItem3());
            question4.setText(subDataBean.getItem4());
        }
        int num=Integer.parseInt(subDataBean.getAnswer());
        String  answer="";
        switch (num){
            case 1:
                answer=subDataBean.getItem1();
                break;
            case 2:
                answer=subDataBean.getItem2();
                break;
            case 3:
                answer=subDataBean.getItem3();
                break;
            case 4:
                answer=subDataBean.getItem4();
                break;
            default:
                break;
        }
        final String finalAnswer = answer;
        one_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  if (subDataBean.getAnswer().equals("1")){
                      //Toast.makeText(activity,"答案正确",Toast.LENGTH_SHORT).show();
                      img_1.setImageResource(R.drawable.true1);
                      img_2.setImageResource(R.drawable.default1);
                      img_3.setImageResource(R.drawable.default1);
                      img_4.setImageResource(R.drawable.default1);
                      error_info.setVisibility(View.GONE);
                      true_info.setVisibility(View.VISIBLE);
                      jieshi_info.setVisibility(View.GONE);
                  }else {
                      saveErrorTiMu(subDataBean);
                      //Toast.makeText(activity,"答案错误",Toast.LENGTH_SHORT).show();
                      img_1.setImageResource(R.drawable.false1);
                      img_2.setImageResource(R.drawable.default1);
                      img_3.setImageResource(R.drawable.default1);
                      img_4.setImageResource(R.drawable.default1);
                      error_info2.setText("您答错啦！正确答案是："+ finalAnswer);
                      error_info.setVisibility(View.VISIBLE);
                      jieshi_info.setVisibility(View.VISIBLE);
                      jieshi_info2.setText(subDataBean.getExplain());
                      true_info.setVisibility(View.GONE);
                  }
            }
        });

        one_lin2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (subDataBean.getAnswer().equals("2")){
                    //Toast.makeText(activity,"答案正确",Toast.LENGTH_SHORT).show();
                    img_2.setImageResource(R.drawable.true1);
                    img_1.setImageResource(R.drawable.default1);
                    img_3.setImageResource(R.drawable.default1);
                    img_4.setImageResource(R.drawable.default1);
                    error_info.setVisibility(View.GONE);
                    true_info.setVisibility(View.VISIBLE);
                    jieshi_info.setVisibility(View.GONE);
                }else {
                    saveErrorTiMu(subDataBean);
                    img_2.setImageResource(R.drawable.false1);
                    img_1.setImageResource(R.drawable.default1);
                    img_3.setImageResource(R.drawable.default1);
                    img_4.setImageResource(R.drawable.default1);
                    error_info2.setText("您答错啦！正确答案是："+ finalAnswer);
                    error_info.setVisibility(View.VISIBLE);
                    true_info.setVisibility(View.GONE);
                    jieshi_info.setVisibility(View.VISIBLE);
                    jieshi_info2.setText(subDataBean.getExplain());
                }
            }
        });
        one_lin3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (subDataBean.getAnswer().equals("3")){
                    img_3.setImageResource(R.drawable.true1);
                    img_1.setImageResource(R.drawable.default1);
                    img_2.setImageResource(R.drawable.default1);
                    img_4.setImageResource(R.drawable.default1);
                    error_info.setVisibility(View.GONE);
                    true_info.setVisibility(View.VISIBLE);
                    jieshi_info.setVisibility(View.GONE);
                    //Toast.makeText(activity,"答案正确",Toast.LENGTH_SHORT).show();
                }else {
                    saveErrorTiMu(subDataBean);
                    img_3.setImageResource(R.drawable.false1);
                    img_1.setImageResource(R.drawable.default1);
                    img_2.setImageResource(R.drawable.default1);
                    img_4.setImageResource(R.drawable.default1);
                    error_info2.setText("您答错啦！正确答案是："+ finalAnswer);
                    error_info.setVisibility(View.VISIBLE);
                    true_info.setVisibility(View.GONE);
                    jieshi_info.setVisibility(View.VISIBLE);
                    jieshi_info2.setText(subDataBean.getExplain());
                }
            }
        });
        one_lin4.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (subDataBean.getAnswer().equals("4")){
                    img_4.setImageResource(R.drawable.true1);
                    img_1.setImageResource(R.drawable.default1);
                    img_2.setImageResource(R.drawable.default1);
                    img_3.setImageResource(R.drawable.default1);
                    //Toast.makeText(activity,"答案正确",Toast.LENGTH_SHORT).show();
                    error_info.setVisibility(View.GONE);
                    true_info.setVisibility(View.VISIBLE);
                    jieshi_info.setVisibility(View.GONE);
                }else {
                    saveErrorTiMu(subDataBean);
                    img_4.setImageResource(R.drawable.false1);
                    img_1.setImageResource(R.drawable.default1);
                    img_2.setImageResource(R.drawable.default1);
                    img_3.setImageResource(R.drawable.default1);
                    error_info2.setText("您答错啦！正确答案是："+ finalAnswer);
                    error_info.setVisibility(View.VISIBLE);
                    true_info.setVisibility(View.GONE);
                    jieshi_info.setVisibility(View.VISIBLE);
                    jieshi_info2.setText(subDataBean.getExplain());
                }
            }
        });
    }

    private void  saveErrorTiMu(KeMuYiPoJo.ResultBean subDataBean){
        List<AllErrorPoJp> errorPoJps = LitePal.where("question = ? and  url=?",
                subDataBean.getQuestion(),subDataBean.getUrl()).find(AllErrorPoJp.class);
        if (errorPoJps.isEmpty()){
            //之前没有添加
            AllErrorPoJp album = new AllErrorPoJp();
            album.setAnswer(subDataBean.getAnswer());
            album.setChapter(subDataBean.getChapter());
            album.setExplain(subDataBean.getExplain());
            album.setQuestion(subDataBean.getQuestion());
            album.setType(subDataBean.getType());
            album.setUrl(subDataBean.getUrl());
            album.setItem1(subDataBean.getItem1());
            album.setItem2(subDataBean.getItem2());
            album.setItem3(subDataBean.getItem3());
            album.setItem4(subDataBean.getItem4());
            album.save();
        }else {
            //不需要在添加
            Log.e("678","我已经添加了，不需要在添加了");
        }
        Toast.makeText(activity,"已添加到我的错题",Toast.LENGTH_SHORT).show();
    }

}
